import { defineStore } from "pinia"
import { nanoid } from 'nanoid'
import axios from "axios"
import { reactive } from "vue"



//选项式写法
// export const useLovetalk2 = defineStore("talk", {
//     //actions 里面放置的是一个一个的方法，用于响应组件中的“动作”
//     actions: {
//         async getLovetalk() {
//             // let axi = await axios.get("https://api.uomg.com/api/rand.qinghua?format=json")
//             let obj = { id: nanoid(), title: nanoid() }
//             this.Lovetalk.unshift(obj)
//         }
//     },
//     state() {
//         return {
//             Lovetalk: JSON.parse(localStorage.getItem("talkdata") as string) || []
//         }
//     }
// })


//组合式写法
export const useLovetalk = defineStore("talk", () => {
    //相当于state
    const Lovetalk = reactive(JSON.parse(localStorage.getItem("talkdata") as string) || [])
    //相当于actions
    async function getLovetalk() {
        // let axi = await axios.get("https://api.uomg.com/api/rand.qinghua?format=json")
        let obj = { id: nanoid(), title: nanoid() }
        Lovetalk.unshift(obj)
    }
    return {Lovetalk,getLovetalk}
})